package com.jse;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.charset.Charset;
import java.util.Properties;

import com.jse.jdbc.Jdbc;
import com.jse.json.JsonObject;
import com.jse.util.PathMatcher;

public class Jse {

    private static Log log=new Log("jse");

    public final static JsonObject conf=new JsonObject();//默认配置文件
    public final static Cache<String,Object> cache=new Cache<>(1024);//默认缓存 1024条
    public final static PathMatcher MATCHER=new PathMatcher();//路径匹配器
    public final static String home=System.getProperty("java.home").replace('\\','/');//jdk home
    public final static String dir=System.getProperty("user.dir").replace('\\','/');//当前目录 jar运行时
    public final static String tmpdir=System.getProperty("java.io.tmpdir").replace('\\','/');//系统临时文件目录
    public final static boolean isWindows=File.separatorChar=='\\';
    public final static String VERSION="1.0.0";

    public static String log_level="DEBUG";
    public static String server_type="jdk";
    public static int server_port=80;
    public static String encoding="UTF-8";
    public static Class<?> clazz=Jse.class;
    public static Charset charset;
    
    public static String name;//应用名
    public static File tempdir;//临时文件目录File
    public static String classpath;//类目录
    public static String jspath;//脚本目录
    public static String webapps;//web根目录 /tomcat/webapps
    public static String webapp;//web app目录
	public static boolean runing;

    public static String jspath(){return jspath;}
    public static String webapps() { return webapps;}
    public static String webapp() { return webapp;}
    public static String jspath(String serverName) { return jspath;}
    public static String webapp(String serverName) { return webapp;}
    public static void setWebapp(String web){webapp=web;}
    public static void setWebapps(String web){webapps=web;}

    public static void app(Class<?> c,String[] args) throws Exception {
        clazz=c;
        if(classpath==null) {
//        	Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        classpath=c.getResource("").getPath();
			if(isWindows){
				if(classpath.charAt(0)=='/')classpath=classpath.substring(1);
				if(classpath.startsWith("file:"))classpath=classpath.substring(6);
			}else {
				if(classpath.startsWith("file:"))classpath=classpath.substring(5);
			}
			if(classpath.endsWith("/com/jse/"))classpath=classpath.substring(0,classpath.length()-9);
			if(classpath.contains("/WEB-INF/lib/")) {
				classpath=classpath.substring(0,classpath.indexOf("/WEB-INF/lib/"))+"/WEB-INF/classes";
			}
        }
		System.out.println("""
					========================
					jse version:%s %s
					========================""".formatted(VERSION,classpath));
        if(Files.exists(Path.of(dir,"jse.properties"))){//优先从当前目录取配置
                var prop=new Properties();
				prop.load(new java.io.FileInputStream(dir+"/jse.properties"));conf.putAll(prop);
		}
        else if(Files.exists(Path.of(classpath,"jse.properties"))){
            var prop=new Properties();
			prop.load(new FileInputStream(classpath+"/jse.properties"));conf.putAll(prop);
		}
         else {
			try(var in=c.getResourceAsStream("/jse.properties")){
                    var prop=new Properties();
					prop.load(in);conf.putAll(prop);
			}
		}
        jspath=conf.getString("jse.jspath",dir+"/src");
        if(!Files.exists(Path.of(jspath))){
        	jspath=classpath;
        }
        webapps=conf.getString("jse.webapps",webapps);
        if(webapps==null||!Files.exists(Path.of(webapps))){
        	webapps=dir;
        }
        webapp=conf.getString("jse.webapp",webapp);
        if(webapp==null||!Files.exists(Path.of(webapp))){
        	webapp=dir+"/web";
        }
        name=jspath.replace("/WEB-INF/classes","").replace("/src","");;
		name=name.substring(name.lastIndexOf("/")+1);
		Ioc.reg("cache",cache);
		Jdbc.start();
		Ioc.BEANS.forEach((name,bean)->{
			if(name.indexOf('.')==-1)Js.set(name, bean);
		});
        Server.await();
    }


    public static void main(String[] args) throws Exception {
        app(Jse.class,args);
    }



    
}